<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>模块管理引擎</title>
  </head>
  <body></body>
  <script>
    //AMD require.js  CMD  sea.js
    //COMMON.JS  node.js
    //模块管理引擎
    let module = (function() {
      //存放模块容器
      const moduleList = {};

      function define(name, modules, action) {
        //拿出模块
        modules.map((name, i) => {
          modules[i] = moduleList[name];
        });
        //压入模块
        moduleList[name] = action.apply(null, modules);
        console.log(moduleList);
      }
      return { define };
    })();
    //定义模块
    module.define("hd", [], function() {
      //初始化模块时执行一次
      // console.log("houdunren");
      return {
        first(arr) {
          return arr[0];
        },
        max(arr, key) {
          return arr.sort((a, b) => b[key] - a[key])[0];
        }
      };
    });
    //依赖模块
    module.define("lesson", ["hd"], function(hd) {
      let data = [
        { name: "js", price: 199 },
        { name: "mysql", price: 78 }
      ];
      console.log(hd.max(data, "price"));
      console.log(hd.first(data).price);
    });
  </script>
</html>
